$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'erp/boxinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'boxInfoId', name: 'boxInfoId', index: 'box_info_id', width: 50, key: true,hidden:true },
			{ label: '盒式编码', name: 'boxCode', index: 'box_code', width: 80 }, 			
			{ label: '盒式名称', name: 'boxName', index: 'box_name', width: 80 }, 			
			{ label: '用料方式', name: 'useMethod', index: 'use_method', width: 80 }, 			
			{ label: '最低价', name: 'minPrice', index: 'min_price', width: 80 }, 			
			{ label: '禁止双驳(1:是0:否)', name: 'isForbiden', index: 'is_forbiden', width: 80 }, 			
			{ label: '是否连续新增(1:是0:否)', name: 'isAdd', index: 'is_add', width: 80 }, 			
			{ label: '报价公式', name: 'priceFormula', index: 'price_formula', width: 80 }, 			
			{ label: '重量公式', name: 'weightFormula', index: 'weight_formula', width: 80 }, 			
			{ label: '面积公式', name: 'areaFormula', index: 'area_formula', width: 80 }, 			
			{ label: '核算内容', name: 'checkContent', index: 'check_content', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		boxInfo: {},
		useMethod: '一块无横 竖之分',
		isForbiden: false,
		isAdd: false,
		formulaVisible: false,
		formulaVisible2: false,
		formulaVisible3: false,
		formulaVisible4: false,
		formulaVisible5: false,
		formulaVisible6: false,
		formulaVisible7: false,
		priceFormula: '',
		areaFormula: '',
		weightFormula: '',
		materialLongOne: '',
		materialWideOne: '',
		materialLongTwo: '',
		materialWideTwo: ''
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.boxInfo = {};
		},
		update: function (event) {
			var boxInfoId = getSelectedRow();
			if(boxInfoId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(boxInfoId)
		},
		saveOrUpdate: function (event) {
			vm.boxInfo.priceFormula = vm.priceFormula;
            vm.boxInfo.areaFormula = vm.areaFormula;
            vm.boxInfo.weightFormula = vm.weightFormula;
            vm.boxInfo.materialLongOne = vm.materialLongOne;
            vm.boxInfo.materialWideOne = vm.materialWideOne;
    		vm.boxInfo.materialLongTwo = vm.materialLongTwo;
    		vm.boxInfo.materialWideTwo = vm.materialWideTwo;
			vm.boxInfo.useMethod = vm.useMethod;
			vm.boxInfo.isForbiden = vm.isForbiden;
			vm.boxInfo.isAdd = vm.isAdd;
			var url = vm.boxInfo.boxInfoId == null ? "erp/boxinfo/save" : "erp/boxinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.boxInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var boxInfoIds = getSelectedRows();
			if(boxInfoIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "erp/boxinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(boxInfoIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(boxInfoId){
			$.get(baseURL + "erp/boxinfo/info/"+boxInfoId, function(r){
                vm.boxInfo = r.boxInfo;
                vm.priceFormula = r.boxInfo.priceFormula==null?"":r.boxInfo.priceFormula;
                vm.areaFormula = r.boxInfo.areaFormula==null?"":r.boxInfo.areaFormula;
                vm.weightFormula = r.boxInfo.weightFormula==null?"":r.boxInfo.weightFormula;
                vm.materialLongOne = r.boxInfo.materialLongOne==null?"":r.boxInfo.materialLongOne;
                vm.materialWideOne = r.boxInfo.materialWideOne==null?"":r.boxInfo.materialWideOne;
        		vm.materialLongTwo = r.boxInfo.materialLongTwo==null?"":r.boxInfo.materialLongTwo;
        		vm.materialWideTwo = r.boxInfo.materialWideTwo==null?"":r.boxInfo.materialWideTwo;
        		vm.useMethod = r.boxInfo.useMethod;
    			vm.isForbiden = r.boxInfo.isForbiden;
    			vm.isAdd = vm.boxInfo.isAdd;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		priceFormulaEdit: function(type){
			console.log("start priceFormulaEdit");
		},
		formulaEdit: function(type,parm){
			if(type==1){
				if(parm=='del'){
					this.priceFormula=this.priceFormula.substring(0,this.priceFormula.length-1);
				}else{
					this.priceFormula+=parm;
				}
			}else if(type==2){
				if(parm=='del'){
					this.weightFormula=this.weightFormula.substring(0,this.weightFormula.length-1);
				}else{
					this.weightFormula+=parm;
				}
			}else if(type==3){
				if(parm=='del'){
					this.areaFormula=this.areaFormula.substring(0,this.areaFormula.length-1);
				}else{
					this.areaFormula+=parm;
				}
			}else if(type==4){
				if(parm=='del'){
					this.materialLongOne=this.materialLongOne.substring(0,this.materialLongOne.length-1);
				}else{
					this.materialLongOne+=parm;
				}
			}else if(type==5){
				if(parm=='del'){
					this.materialWideOne=this.materialWideOne.substring(0,this.materialWideOne.length-1);
				}else{
					this.materialWideOne+=parm;
				}
			}else if(type==6){
				if(parm=='del'){
					this.materialLongTwo=this.materialLongTwo.substring(0,this.materialLongTwo.length-1);
				}else{
					this.materialLongTwo+=parm;
				}
			}else if(type==7){
				if(parm=='del'){
					this.materialWideTwo=this.materialWideTwo.substring(0,this.materialWideTwo.length-1);
				}else{
					this.materialWideTwo+=parm;
				}
			}
		},
		cancelFormulaEdit: function(type){
			if(type==1){
				this.priceFormula='';
			}else if(type==2){
				this.weightFormula='';
			}else if(type==3){
				this.areaFormula='';
			}else if(type==4){
				this.materialLongOne='';
			}else if(type==5){
				this.materialWideOne='';
			}else if(type==6){
				this.materialLongTwo='';
			}else if(type==7){
				this.materialWideTwo='';
			}
		}
	}
});